#include "UISettingComponent.h"
#include "AppDelegate.h"
#include <Windows.h>
#include "GL/wglew.h"

Size SettingOptionSize;
unsigned int SettingOptionEventFlag;

UICDropdownList * UISettingComponent::optionResolution()
{
	auto ddl = UICDropdownList::create(SettingOptionSize, THEMECOLORB, TEXTCOLORB);
	Size screensize(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
	int resSelectedIndex = 0;
	RECT wndSize;
	//GetWindowRect(Director::getInstance()->getOpenGLView()->getWin32Window(), &wndSize);
	//int wndWidth = wndSize.right - wndSize.left;
	for (int resw = 1920; resw >= 800; resw -= 160)
	{
		auto resh = 9 * resw / 16;
		if (resw >= screensize.width || resh >= screensize.height) continue;
		string resstr = strtool::int2str(resw) + " x " + strtool::int2str(9 * resw / 16);
		ddl->addItem(resstr, resw);
		if (resw > pConfig->getIntValue("graphics", "resw")) resSelectedIndex++;
	}
	ddl->setEventFlag(SettingOptionEventFlag);
	ddl->setAdditionalEventAction(EventHelper::MOUSEDOWN, [=]
	{
		pConfig->setIntValue("graphics", "resw", ddl->getSelectedTag());
		UI::setWndSize(ddl->getSelectedTag());
	});
	ddl->setSelectedIndex(resSelectedIndex);
	return ddl;
}

UICDropdownList * UISettingComponent::optionMultisample()
{
	auto ddl = UICDropdownList::create(SettingOptionSize, THEMECOLORB, TEXTCOLORB);
	int msaaSelectedIndex = 0;
	for (int msaa = 16; msaa > 1; msaa /= 2)
	{
		string msaastr = strtool::int2str(msaa) + "x";
		ddl->addItem(msaastr, msaa);
		if (msaa > pConfig->getIntValue("graphics", "msaa")) msaaSelectedIndex++;
	}
	ddl->addItem("OFF", 1);
	ddl->setEventFlag(SettingOptionEventFlag);
	ddl->setAdditionalEventAction(EventHelper::MOUSEDOWN, [=]
	{
		pConfig->setIntValue("graphics", "msaa", ddl->getSelectedTag());
		glfwWindowHint(GLFW_SAMPLES, ddl->getSelectedTag());
	});
	ddl->setSelectedIndex(msaaSelectedIndex);
	return ddl;
}

UICSwitchOnOff * UISettingComponent::optionVsync()
{
	auto vsync = UICSwitchOnOff::create(SettingOptionSize, THEMECOLORB, TEXTCOLORB, false, "ENABLE", "DISABLE");
	vsync->setEventFlag(SettingOptionEventFlag);
	vsync->setAdditionalEventAction(EventHelper::MOUSEDOWN, [=]
	{
		pConfig->setIntValue("graphics", "vsync", vsync->getStatus() ? 1 : 0);
		AppDelegate::setGLVsync(vsync->getStatus());
	});
	vsync->setStatus(pConfig->getIntValue("graphics", "vsync") == 0 ? false : true);
	return vsync;
}

UICDropdownList * UISettingComponent::optionFpsLimit()
{
	auto ddl = UICDropdownList::create(SettingOptionSize, THEMECOLORB, TEXTCOLORB);
	int selectedIndex = 0;
	for (int fl = 240; fl >= 60; fl /= 2)
	{
		string flstr = strtool::int2str(fl);
		ddl->addItem(flstr, fl);
		if (fl > pConfig->getIntValue("graphics", "fpslimit")) selectedIndex++;
	}
	ddl->addItem("UNLIMITED", 0);
	ddl->setEventFlag(SettingOptionEventFlag);
	ddl->setAdditionalEventAction(EventHelper::MOUSEDOWN, [=]
	{
		pConfig->setIntValue("graphics", "fpslimit", ddl->getSelectedTag());
		auto interval = 1.0 / (ddl->getSelectedTag() > 0 ? ddl->getSelectedTag() : 9999);
		pDirector->setAnimationInterval(interval);
		//pDirector->restart();
	});
	ddl->setSelectedIndex(selectedIndex);
	return ddl;
}

UICSliderBar * UISettingComponent::optionMasterVolume()
{
	auto val = pConfig->getIntValue("audio", "volmaster");
	auto mvol = UICSliderBar::create(SettingOptionSize, THEMECOLORBA(0), TEXTCOLORB, 0, 100, 1, val, 0);
	mvol->setEventFlag(SettingOptionEventFlag);
	mvol->setOnValueChangeAction([=]
	{
		pConfig->setFloatValue("audio", "volmaster", mvol->getValue(), "value", false);
		pAudioSystem->setMasterVolume(mvol->getValue() / 100.f);
	});
	mvol->setAdditionalEventAction(EventHelper::MOUSEUP, [=]
	{
		pConfig->saveConfig();
	});
	return mvol;
}

UICSliderBar * UISettingComponent::optionMusicVolume()
{
	auto val = pConfig->getIntValue("audio", "volmusic");
	auto mvol = UICSliderBar::create(SettingOptionSize, THEMECOLORBA(0), TEXTCOLORB, 0, 100, 1, val, 0);
	mvol->setEventFlag(SettingOptionEventFlag);
	mvol->setOnValueChangeAction([=]
	{
		pConfig->setFloatValue("audio", "volmusic", mvol->getValue(), "value", false);
		pAudioSystem->setChannelVolume(pAudioSystem->curbgm.pChl, 0, mvol->getValue() / 100.f);
	});
	mvol->setAdditionalEventAction(EventHelper::MOUSEUP, [=]
	{
		pConfig->saveConfig();
	});
	return mvol;
}

